package com.lhb.email.consumer;

import com.lhb.email.model.SendCodeModel;
import com.lhb.email.model.SendNewPassswordModel;
import com.lhb.email.service.EmailService;
import jakarta.mail.MessagingException;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

/**
 * @author 86131
 * @version 1.0
 * @description
 * @date 2025/7/28 下午8:47
 */
@Component
public class EmailConsumer {
    @Autowired
    private EmailService emailService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "register.queue1"),
            exchange = @Exchange(name = "email.direct", type = ExchangeTypes.DIRECT),
            key = {"register"}
    ))
    public void listenRegisterQueue1(String email) throws MessagingException, UnsupportedEncodingException {
        emailService.sendRegisterInfoToEmail(email);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "code.queue1"),
            exchange = @Exchange(name = "email.direct", type = ExchangeTypes.DIRECT),
            key = {"code"}
    ))
    public void listenCodeQueue1(SendCodeModel sendCodeModel) throws MessagingException, UnsupportedEncodingException {
        emailService.sendVerificationEmail(sendCodeModel.getEmail(),sendCodeModel.getCode());
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "resetPassword.queue1"),
            exchange = @Exchange(name = "email.direct", type = ExchangeTypes.DIRECT),
            key = {"resetPassword"}
    ))
    public void listenResetPasswordQueue1(SendNewPassswordModel sendNewPassswordModel) throws MessagingException, UnsupportedEncodingException {
        emailService.sendNewPasswordEmail(sendNewPassswordModel.getEmail(),sendNewPassswordModel.getPassword());
    }

}
